﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
#endregion

namespace GameJolt.Requests
{
	/// <summary>
	/// 
	/// </summary>
	internal sealed class ImageRequest : WebRequestBase<Stream>
	{
		#region Constructors
		public ImageRequest(string url)
			: base(null, null)
		{
			this.UrlBase = url;
		}
		#endregion

		#region Methods
		public override Stream Process(IAsyncResult result)
		{
			base.Process(result);
			var request = (HttpWebRequest)WebRequest.Create(UrlBase);
			using(var response = request.GetResponse())
			{
				using(var reader = new BinaryReader(response.GetResponseStream()))
				{
					var output = new List<byte>();

					byte[] buffer;
					while((buffer = reader.ReadBytes(1024)).Length > 0)
						output.AddRange(buffer);

					return new MemoryStream(output.ToArray());
				}
			}
		}
		#endregion
	}
}
